package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcDeductDAO;
import com.trkj.dao.jpa.CcDeductdetailDAO;
import com.trkj.dao.jpa.CcElectornicCheckDAO;
import com.trkj.dao.jpa.CcElectornicPrescribeDAO;
import com.trkj.dao.jpa.CcHospitalElectronicDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.dao.jpa.IJlCheckProjectDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.pojos.CcDeduct;
import com.trkj.pojos.CcDeductdetail;
import com.trkj.pojos.CcElectornicCheck;
import com.trkj.pojos.CcHospitalElectronic;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcPrepayment;
import com.trkj.pojos.JlCheckProject;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcElectornicCheckVo;

@Service
@Transactional
public class CcHospEleCheckService {

	@Autowired
	private CcElectornicCheckDAO ccelecDAO;
	@Autowired
	private CcHospitalElectronicDAO cchospeleDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcPatientDAO patientDAO;
	@Autowired
	private IJlCheckProjectDAO jlCheckDAO;
	@Autowired
	private CcPrepaymentDAO prepaymentDAO;
	@Autowired
	private CcDeductDAO ccDeductDAO;
	@Autowired
	private CcDeductdetailDAO deductdetailDAO;
	
	public Map<String,Object> queryByAll(int page,int rows){
		Map<String,Object> map= new HashMap<>();
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcElectornicCheck> list=ccelecDAO.queryByAll(pr);
		map.put("total", ccelecDAO.count());
		map.put("rows", ccElectornicCheckToCcElectornicCheckVo(list));
		return map;
	}
	public void updateBycheckid(String checkid,String lujing){
		CcElectornicCheck elecheck=ccelecDAO.findOne(Long.valueOf(checkid));
		CcHospitalElectronic hospele=cchospeleDAO.findOne(elecheck.getEleTid().longValue());
		CcPatient cp=patientDAO.findOne(hospele.getHeSickid().longValue());
		CcPrepayment pp=prepaymentDAO.findBypatient(new BigDecimal(cp.getPatientid()));
		JlCheckProject cc_check=jlCheckDAO.findOne(elecheck.getElecname().longValue());
		elecheck.setCheckrameks("已完成");
		elecheck.setPhotofruit(lujing);
		ccelecDAO.save(elecheck);
		pp.setPpmoney(pp.getPpmoney().subtract(elecheck.getEtmenoy()));
		prepaymentDAO.save(pp);
		CcDeduct cdd=new CcDeduct();
		cdd.setDedate(new Date());
		cdd.setDmoney(elecheck.getEtmenoy());
		cdd.setDpaymentid(new BigDecimal(pp.getPpaymentid()));
		
		ccDeductDAO.save(cdd);
		CcDeductdetail cddd=new CcDeductdetail();
		cddd.setDddate(new Date());
		cddd.setDdeductid(new BigDecimal(cdd.getDeductid()));
		cddd.setDdmoney(cdd.getDmoney());
		cddd.setDdname(cc_check.getCprProject());
		deductdetailDAO.save(cddd);
		List<CcElectornicCheck> list=ccelecDAO.queryByEleId(elecheck.getEleTid());
		boolean jg=false; 
		for(CcElectornicCheck cec:list){
			if(cec.getCheckrameks()==null){
				jg=true;
				break;
			}
		}
		if(jg==false){
			hospele.setHospeleRemarks("已完成");
			cchospeleDAO.save(hospele);
		}else{
			hospele.setHospeleRemarks("进行中");
			cchospeleDAO.save(hospele);
		}
		
		
	}
	public List<CcElectornicCheckVo> queryByicid(String icid){
		CcPatient cp=patientDAO.findByOneCcPatient(new BigDecimal(icid));
		List<CcElectornicCheckVo> listVo=new ArrayList<CcElectornicCheckVo>();
		//System.err.println("1232131332");
		if(cp!=null){
			List<CcHospitalElectronic> list=cchospeleDAO.findByheSickid(null, new BigDecimal(cp.getPatientid()));
			//System.err.println(cp.getPname());
			for(CcHospitalElectronic ehosp:list){
				if(ehosp.getHeType().equals("检查")){
					//System.err.println(ehosp.getHeDate());
					List<CcElectornicCheck> hospcheck=ccelecDAO.queryByEleId(new BigDecimal(ehosp.getHeId()));
					listVo=ccElectornicCheckToCcElectornicCheckVo(hospcheck);
				}
			}
		}
		return listVo;
	}
	public List<CcElectornicCheckVo> ccElectornicCheckToCcElectornicCheckVo(List<CcElectornicCheck> list){
		List<CcElectornicCheckVo> listVo=new ArrayList<CcElectornicCheckVo>();
		for(CcElectornicCheck cec:list){
			CcElectornicCheckVo cecVo=new CcElectornicCheckVo();
			cecVo.setEcheckid(cec.getEcheckid());
			cecVo.setEleTid(cec.getEleTid());
			cecVo.setEtmenoy(cec.getEtmenoy());
			cecVo.setElecname(cec.getElecname());
			if(cecVo.getElecname()!=null){
				JlCheckProject check=jlCheckDAO.findOne(cecVo.getElecname().longValue());
				cecVo.setEcheckName(check.getCprProject());
				cecVo.setElecheckid(check.getCprId());
			}
			cecVo.setEtmenoy(cec.getEtmenoy());
			CcHospitalElectronic cchospele=cchospeleDAO.findOne(cecVo.getEleTid().longValue());
			cecVo.setHe_date(cchospele.getHeDate());
			cecVo.setCastate(cchospele.getHospeleRemarks());
			if(cec.getCheckrameks()==null){
				cecVo.setCheckrameks("未完成");
			}else{
				cecVo.setCheckrameks(cec.getCheckrameks());
			}
			CcPatient ccpatient=patientDAO.findOne(cchospele.getHeSickid().longValue());
			cecVo.setPid(ccpatient.getPatientid());
			cecVo.setPname(ccpatient.getPname());
			Tbstaff staff=staffDAO.findOne(cchospele.getHeDoctorid().longValue());
			cecVo.setStaffid(staff.getStaffNo());
			cecVo.setStaffName(staff.getStaffName());
			listVo.add(cecVo);
		}
		
		return listVo;
	}
}
